04. Edge Case: Singleton Anti-pattern

Edge Case: Singleton Anti-pattern

ND079 JPND C2 L03 A05 Edge Case Singleton Anti Pattern

Disadvantages of Singletons

If you misuse the singleton pattern, it can be detrimental to your software design.

  • Singletons can cause brittle assumptions in your code. If later on, you decide you need multiple instances of the class, you might have to refactor a bunch of code.
  • Code that depends on singletons can become hard to test.

If you're itching for a code example of how the singleton pattern can result in difficult-to-test code, and how Dependency Injection can improve testability, you can look forward to the Dependency Injection exercise near the end of this lesson!

What are some disadvantages to using the singleton pattern?

SOLUTION:
  • Your code may be hard to refactor if you ever need multiple instances.
  • Code that depends on singletons can be hard to test.